package com.ruoyi.project.per.emp.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.per.emp.domain.Emp;
import com.ruoyi.project.per.emp.domain.EmpPost;
import com.ruoyi.project.per.emp.mapper.EmpMapper;
import com.ruoyi.project.per.emp.mapper.EmpPostMapper;
import com.ruoyi.project.system.post.mapper.PostMapper;
import com.ruoyi.project.system.role.mapper.RoleMapper;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.domain.UserPost;
import com.ruoyi.project.system.user.mapper.UserMapper;
import com.ruoyi.project.system.user.mapper.UserPostMapper;
import com.ruoyi.project.system.user.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.remote.rmi._RMIConnection_Stub;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 小新
 * @date 2020-09-17 12:23
 * @desc
 */
@Service
public class EmpServiceImpl implements IEmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private EmpPostMapper empPostMapper;


    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<Emp> selectEmpList(Emp emp) {
        // 生成数据权限过滤条件
        return empMapper.selectEmpList(emp);
    }

    @Override
    public String checkPhoneUnique(Emp emp) {

            Long empId = StringUtils.isNull(emp.getEmpId()) ? -1L : emp.getEmpId();
            Emp info = empMapper.checkPhoneUnique(emp.getPhonenumber());
            if (StringUtils.isNotNull(info) && info.getEmpId().longValue() != empId.longValue())
            {
                return UserConstants.USER_EMAIL_NOT_UNIQUE;
            }
            return UserConstants.USER_EMAIL_UNIQUE;
        }


    @Override
    public String checkEmailUnique(Emp emp) {
        Long empId = StringUtils.isNull(emp.getEmpId()) ? -1L : emp.getEmpId();
        Emp info = empMapper.checkEmailUnique(emp.getEmail());
        if (StringUtils.isNotNull(info) && info.getEmpId().longValue() != empId.longValue())
        {
            return UserConstants.USER_EMAIL_NOT_UNIQUE;
        }
        return UserConstants.USER_EMAIL_UNIQUE;
    }

    /**
     * 新增员工岗位信息
     *
     * @param emp 员工对象
     */
    public void insertEmpPost(Emp emp)
    {
        Long[] posts = emp.getPostIds();
        if (StringUtils.isNotNull(posts))
        {
            // 新增员工与岗位管理
            List<EmpPost> list = new ArrayList<EmpPost>();
            for (Long postId : emp.getPostIds())
            {
                EmpPost up = new EmpPost();
                up.setEmpId(emp.getEmpId());
                up.setPostId(postId);
                list.add(up);
            }
            if (list.size() > 0)
            {
                empPostMapper.batchEmpPost(list);
            }
        }
    }

    @Override
    public int insertEmp(Emp emp) {


        emp.setCreateBy(ShiroUtils.getLoginName());
        // 新增用户信息
        int rows = empMapper.insertEmp(emp);
        // 新增员工岗位关联
       insertEmpPost(emp);

        return rows;
    }

    @Override
    public int deleteEmpByIds(String ids) {
        Long[] empIds = Convert.toLongArray(ids);
        // 删除用户与岗位表
        for (int i = 0; i < empIds.length; i++) {
            empPostMapper.deleteEmpPostByEmpId(empIds[i]);
        }

        return empMapper.deleteEmpByIds(empIds);
    }

    @Override
    public Emp selectEmpById(Long empId) {
        return empMapper.selectEmpById(empId);
    }

    /**
     * 保存修改信息
     * @param emp
     * @return
     */
    @Override
    @Transactional
    public int updateEmp(Emp emp) {
        Long empId = emp.getEmpId();
        //修改的时间
        LocalDateTime now = LocalDateTime.now();
        Date updateTime = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
        emp.setUpdateTime(updateTime);
        emp.setUpdateBy(ShiroUtils.getLoginName());


        // 删除用户与岗位关联
        empPostMapper.deleteEmpPostByEmpId(empId);
        // 新增用户与岗位管理
        insertEmpPost(emp);
        return empMapper.updateEmp(emp);
    }


}
